Skip to content

Keep bytes after a row separator split across chunks - #401

Open
adityawrk wants to merge 2 commits into
ruby:mainfrom
adityawrk:fix/crlf-chunk-boundary
Open

adityawrk wants to merge 2 commits into
ruby:mainfrom
adityawrk:fix/crlf-chunk-boundary

Conversation

@adityawrk

@adityawrk adityawrk commented Nov 29, 2026 •

Copy link
Copy Markdown

A two-character row separator can fall across the streaming reader's chunk boundary. The reader glued the separator back together, then left the scanner at the start of the next record while the bytes it had already taken from that chunk lived only in a local buffer. The next read discarded them. A quoted field was then parsed differently from a file than from a string:

# 8192-byte chunks, row_sep: "\r\n", separator at byte 8191
CSV.parse(data, row_sep: "\r\n")
# => CSV::MalformedCSVError: Unclosed quoted field in line 2.

CSV.foreach(path, row_sep: "\r\n") { |row| ... }
# => [["x" * 8191], [], ["admin", "true"]]

The same bytes now produce the same result either way. An unclosed quote is rejected from a file as well as from a string, and a quote that does close round-trips, including the field that begins in the chunk after the split separator.

With a chunk size of 8, the old reader returned [[7], [], [5, 4]] for the unclosed case and raised "Unclosed quoted field in line 4." for the closed case. After the change those are "Unclosed quoted field in line 2." and [[7], [20]]. ruby run-test.rb is 536 tests, 4073 assertions, including the chunk-size-1 scanner run.

Fixes #397

A quoted field after a two-character separator split across a
read chunk was dropped when the input was streamed and kept
when it was parsed in memory.
The scanner rewound to the start of the next record but left
the bytes already read from that chunk only in a local buffer.
The next read discarded them, so a quoted field was parsed
differently from a file than from a string.

Fixes ruby#397

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streaming CSV parser drops records when a two-character row separator crosses a chunk boundary

1 participant